package com.linkhub.oms.dao.mpservice.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linkhub.oms.dao.mapper.SupplierEntityMapper;
import com.linkhub.oms.dao.mpservice.ISupplierService;
import com.linkhub.oms.entity.SupplierEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 供应商表 服务实现类
 * </p>
 *
 * @author echo
 * @since 2024-04-24
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierEntityMapper, SupplierEntity> implements ISupplierService {

    @Override
    public List<SupplierEntity> listByUserId(String userId) {
        return lambdaQuery()
                .eq(SupplierEntity::getUserId, userId)
                .orderByDesc(SupplierEntity::getCreatedTime)
                .list();

    }

    @Override
    public SupplierEntity getBySupplierId(String userId, String sysSupplierId) {
        return lambdaQuery()
                .eq(SupplierEntity::getUserId, userId)
                .eq(SupplierEntity::getSysSupplierId, sysSupplierId)
                .one();
    }

    @Override
    public boolean existSupplier(String userId, String supplierName, String excludeSysSupplierId) {

        return lambdaQuery()
                .eq(SupplierEntity::getUserId, userId)
                .eq(SupplierEntity::getSupplierName, supplierName)
                .ne(StringUtils.isNotBlank(excludeSysSupplierId), SupplierEntity::getSysSupplierId, excludeSysSupplierId)
                .exists();
    }

    @Override
    public boolean existSupplier(String userId, String supplierName) {
        return this.existSupplier(userId, supplierName, null);
    }

    @Override
    public boolean exist(String userId, String sysSupplierId) {
        return lambdaQuery()
                .eq(SupplierEntity::getUserId, userId)
                .eq(SupplierEntity::getSysSupplierId, sysSupplierId)
                .exists();
    }

    @Override
    public List<SupplierEntity> getBySupplierIds(String userId, List<String> sysSupplierIdList) {
        return lambdaQuery()
                .eq(SupplierEntity::getUserId, userId)
                .in(SupplierEntity::getSysSupplierId, sysSupplierIdList)
                .list();
    }
}
